const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    partGroupList: 4,   
  },
  onLoad(options){
    this.setData({
      CarInfo: JSON.parse(options.CarInfo),
      vehicleId: options.vehicleId
    });
    console.log(JSON.parse(options.CarInfo));
    // this.getConstruction(options.vehicleId);
    this.getConstruction();
   
  },
  /*分享*/
  onShareAppMessage(){},

  /*初始数据*/ 
  InitData() {
    let _this;
    if (!_this) {
      _this = this;
    }
    return {
      loading() {
        _this.setData({
          showInitLoading: false,
          RequestStatus: true
        });
      }
    }
  },

  /*获取车辆结构*/
  getConstruction(vehicleId){
    let _this = this,td = this.data;
    if (td.vehicleId) {
      _this.InitData().loading();
      SERVICE.POST(SERVICE.URL.getConstruction, {
        vehicleId: td.vehicleId
      }, (status, res) => {
        console.log("车辆结构:",res);
        if (status) {
          let partGroupList = res.data.res_msg.result.partGroupList;
          for (let i = 0; i < partGroupList.length; i++){
            partGroupList[i].selected = false;
            for (let e = 0; e < partGroupList[i].List.length; e++){
              partGroupList[i].List[e].selected = false;
              
            }
          }
          console.log(partGroupList);
          _this.setData({
            showInitLoading: true,
            partGroupList
          });
        } else {
          _this.setData({
            showInitLoading: true,
            partGroupList: null,
            
          });
        }
      },function fail(){
          _this.setData({
            RequestStatus: false
          });
      });
    }
  },
  
  /*重新加载*/ 
  RequestCellBack() {
    this.getConstruction();
  },
  
  /*获取项对应配件*/ 
  getParts(e){
    let td = this.data, index = e.currentTarget.dataset.index, 
    leaf = e.currentTarget.dataset.leaf, id = e.currentTarget.dataset.id;
    td.partGroupList[index].selected = !td.partGroupList[index].selected;
    if(leaf == false){
      this.setData({
        // showItem: index,
        partGroupList: td.partGroupList
      });
    }else{
      wx.navigateTo({
        url: "../mountings-list/mountings-list?CarInfo=" + JSON.stringify(td.CarInfo) + '?id=' + id
      });
    }
  },

  /*获取相关配件*/
  getRelatedParts(e){
    let td = this.data, item = e.currentTarget.dataset.item, index = e.currentTarget.dataset.index,
    leaf = e.currentTarget.dataset.leaf, id = e.currentTarget.dataset.id;
    console.log(item, index);
    td.partGroupList[item].List[index].selected = !td.partGroupList[item].List[index].selected;
    if(leaf == false){
      this.setData({
        // showItem_2: index,
        partGroupList: td.partGroupList
      });
    }else{
      wx.navigateTo({
        url: "../mountings-list/mountings-list?CarInfo=" + JSON.stringify(td.CarInfo) + '&id=' + id
      });
    }
  },

  getRelatedPartsChild(e){
    let td = this.data, id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../mountings-list/mountings-list?CarInfo=" + JSON.stringify(td.CarInfo)+ '&id=' + id
    });
  },

  /*搜索*/ 
  SearchSubmit(e){
    let td = this.data, search_val = e.detail.value.search;
    if (search_val.length > 0){
      wx.navigateTo({
        url: "../mountings-list/mountings-list?CarInfo=" + JSON.stringify(td.CarInfo) + "&search_val=" + search_val 
      });
    }
  }
});